﻿// -----------------------------------------------------------------------
// <copyright file="TwoImgOperation.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;

    /// <summary>
    /// two image operation
    /// </summary>
    public class TwoImgOperation 
    {
        /// <summary>
        /// static object of this class to use this without create object
        /// </summary>
        private static TwoImgOperation twoImgOperAddOrSub = new TwoImgOperation();

        /// <summary>
        /// buffer of input image
        /// </summary>
        private Color[,] bufferOfImage;

        /// <summary>
        /// Gets or sets the two img oper add or sub.
        /// </summary>
        /// <value>
        /// The two img oper add or sub.
        /// </value>
        public static TwoImgOperation TwoImgOperAddOrSub
        {
            get { return TwoImgOperation.twoImgOperAddOrSub; }
            set { TwoImgOperation.twoImgOperAddOrSub = value; }
        }

        /// <summary>
        /// subtract two image img1-img2 with size of image 1.
        /// </summary>
        /// <param name="img1">The img1.</param>
        /// <param name="img2">second image.</param>
        /// <returns>
        /// new image
        /// </returns>
        public Bitmap Sub(Bitmap img1, Bitmap img2)
        {
            this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(img1);
            double[,,] newBufferOfImage = new double[this.bufferOfImage.GetLength(0), this.bufferOfImage.GetLength(1), 3];
            Bitmap newImg = Resize.Resize1.ResizeI(img2, null, this.bufferOfImage.GetLength(0), this.bufferOfImage.GetLength(1));
            Color[,] bufferImg2 = BufferOfImage.Buffer.GetBuffer(newImg);
            newImg = new Bitmap(this.bufferOfImage.GetLength(0), this.bufferOfImage.GetLength(1));
            try
            {
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        newBufferOfImage[j, i, 0] = this.bufferOfImage[j, i].R - bufferImg2[j, i].R;
                        newBufferOfImage[j, i, 1] = this.bufferOfImage[j, i].G - bufferImg2[j, i].G;
                        newBufferOfImage[j, i, 2] = this.bufferOfImage[j, i].B - bufferImg2[j, i].B;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }

            this.bufferOfImage = Normalize.Normalize11.Normalization(newBufferOfImage);
            return BufferOfImage.Buffer.GetImage(this.bufferOfImage);
        }

        /// <summary>
        /// Adds two image.
        /// </summary>
        /// <param name="img1">The img1.</param>
        /// <param name="img2">image two.</param>
        /// <param name="fraction">The fraction.</param>
        /// <returns>
        /// modified image
        /// </returns>
        public Bitmap Add(Bitmap img1, Bitmap img2, double fraction)
        {
            this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(img1);
            Bitmap newImg = Resize.Resize1.ResizeI(img2, null, this.bufferOfImage.GetLength(0), this.bufferOfImage.GetLength(1));
            Color[,] bufferimg2 = BufferOfImage.Buffer.GetBuffer(newImg);
            newImg = new Bitmap(this.bufferOfImage.GetLength(0), this.bufferOfImage.GetLength(1)); 
            try
            {
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        newImg.SetPixel(j, i, Color.FromArgb((int)Math.Abs(((this.bufferOfImage[j, i].R * fraction) + (bufferimg2[j, i].R * (1 - fraction)))), (int)Math.Abs((this.bufferOfImage[j, i].G * fraction) + (bufferimg2[j, i].G * (1 - fraction))), (int)Math.Abs((this.bufferOfImage[j, i].B * fraction) + (bufferimg2[j, i].B * (1 - fraction)))));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }

            return newImg;
        }
    }
}
